#include<iostream>
#include<string>
#include</usr/include/websocketpp/server.hpp>
#include</usr/include/websocketpp/config/asio_no_tls.hpp>

typedef websocketpp::server<websocketpp::config::asio> wsserver_t;
void open_callback(websocketpp::connection_hdl hdl)
{
    std::cout<<"websocket握手成功！！"<<std::endl;
}
void close_callback(websocketpp::connection_hdl hdl)
{
    std::cout<<"websocket连接断开！！"<<std::endl;
}
void http_callback(wsserver_t* wsser,websocketpp::connection_hdl hdl)
{
    wsserver_t::connection_ptr conn=wsser->get_con_from_hdl(hdl);
    std::cout<<conn->get_request_body()<<std::endl;
    websocketpp::http::parser::request req=conn->get_request();
    std::cout<<req.get_method()<<std::endl;
    std::cout<<req.get_uri()<<std::endl;

    //组织回复
    std::string body="hhhhhhhhh";
    conn->set_body(body);
    conn->append_header("Content-Type","text/html");
    conn->set_status(websocketpp::http::status_code::value::ok);
}
void messager_callback(wsserver_t* wsser, websocketpp::connection_hdl hdl,wsserver_t::message_ptr ptr)
{
    wsserver_t::connection_ptr conn=wsser->get_con_from_hdl(hdl);
    std::string const& messag=ptr->get_payload();
    std::cout<<"messager :"<<messag<<std::endl;
    std::string resp="server say "+messag;
    conn->send(resp);//默认是文本类型
}
int main()
{
    //1.实例化一个websocket服务器
    //websocketpp::server<websocketpp::config::asio> wsser;
    wsserver_t wsser;
    //2.设置日志输出等级：
    wsser.set_access_channels(websocketpp::log::alevel::none);
    //3.初始化服务器
    wsser.init_asio();
    //4.设置回调函数
    wsser.set_open_handler(open_callback);
    wsser.set_close_handler(close_callback);
    wsser.set_http_handler(bind(http_callback,&wsser,std::placeholders::_1));
    wsser.set_message_handler(bind(messager_callback,&wsser,std::placeholders::_1, std::placeholders::_2));
    //5.设置监听端口
    wsser.listen(8080);
    //6.获取新连接
    wsser.start_accept();
    //7.运行服务器
    wsser.run();
    return 0;
}